#include "fun.h"
#include "onewire.h"
#include "ds1302.h"

u8 bdata LED;
sbit LED1 = LED^0;
u8 key_count,index,L_flag,i,t_200 = 1,t_100 = 1,key_flag = 1;
u16 led_time;
u8 code t_display[]={                       //标准字库
//   0    1    2    3    4    5    6    7    8    9    C     -
    0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x39,0x40,0x00};    

u8 T_COM[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};      //位码
u8 data_disp[][8] = 
{
    {0,0,11,0,0,11,0,0},
    {12,12,12,12,12,0,0,10},
    {0,0,11,0,0,11,0,0}
};

void Timer2Init(void)		//1毫秒@12.000MHz
{
	AUXR |= 0x04;		//定时器时钟1T模式
	T2L = 0x20;		//设置定时初值
	T2H = 0xD1;		//设置定时初值
	AUXR |= 0x10;
    IE2 |= 0x04;
    EA = 1;
}

void smg_pro(void)
{
    if(!disp_status)        //时钟显示
    {
        if(!t_flag && tem_flag && !key_up)
        {
            index = 1;
            data_disp[index][5] = temperature / 10;
            data_disp[index][6] = temperature % 10;
        }
        else
        {
						if(!t_flag && key_up)
							tem_flag = 0;
            index = 0;
            data_disp[index][0] = t_h / 10;
            data_disp[index][1] = t_h % 10;
            data_disp[index][3] = t_m / 10;
            data_disp[index][4] = t_m % 10;
            data_disp[index][6] = t_s / 10;
            data_disp[index][7] = t_s % 10;
        }
    }
    else if(disp_status == 1)       //闹钟设置
    {
        index = 2;
        data_disp[index][0] = a_h / 10;
        data_disp[index][1] = a_h % 10;
        data_disp[index][3] = a_m / 10;
        data_disp[index][4] = a_m % 10;
        data_disp[index][6] = a_s / 10;
        data_disp[index][7] = a_s % 10;
    }
}


void main(void)
{
		u8 temp;
    buzzer(0);
    relay(0);
    led(LED);
    rd_temperature();
    clock_init(t_h,t_m,t_s);
    Timer2Init();
	
    while(1)
    {
		if(key_flag)
		{
			key_flag = 0;
			key_scan();
		}
	
		if(t_100)
		{
			t_100 = 0;
			temp = Read_Ds1302_Byte(0x85);
			t_h = (temp / 16) * 10 + (temp % 16);
			temp = Read_Ds1302_Byte(0x83);
			t_m = (temp / 16) * 10 + (temp % 16);
			temp = Read_Ds1302_Byte(0x81);
			t_s = (temp / 16) * 10 + (temp % 16);
		}	
        
        if(t_200)
        {
            t_200 = 0;
            rd_temperature();
        }
        smg_pro();

        if((!led_flag) && (t_h == a_h) && (t_m == a_m) && (t_s == a_s))  //检测闹钟启动条件
            led_flag = 1;

        if(t_flag || a_flag)
        {
            if((flag && (t_s % 2)) || (!flag && (!(t_s % 2))))
                L_flag = 1;
            else
                L_flag = 0;
        }
        else
            L_flag = 1;
    }
}

void T2_callback(void) interrupt 12
{
    static u8 t = 0;
    t++;
    i %= 8;
    if(++key_count == 10)
    {
				key_flag = 1;
        key_count = 0;
    }
    
    if(t >= 200)
    {
				t_100 = 1;
        t_200 = 1;
        t = 0;
    }
		else if(t == 100)
			  t_100 = 1;

    if(led_flag == 1)        //灯闪5秒
    {
        led_time++;
        if(led_time == 5000)
        {
            led_time = 0;
            led_flag = 0;
            LED1 = 0;
            led(LED);
        }
        else if(!(led_time % 200))
        {
            LED1 = ~LED1;
            led(LED);
        }
    }
    else if(led_flag == 2)
    {
        led_time++;
        if(led_time >= 1000)
        {
            led_flag = 0;
            led_time = 0;
        }
        LED1 = 0;
        led(LED);
    }

    T_COM[i] = 0x01 << i;
    if(t_flag == 1 || a_flag == 1)
    {
        if(!L_flag && (i == 0 || i == 1))
            T_COM[i] = 0; 
    }
    else if(t_flag == 2 || a_flag == 2)
    {
        if(!L_flag && (i == 3 || i == 4))
            T_COM[i] = 0;
    }
    else if(t_flag == 3 || a_flag == 3)
    {
        if(!L_flag && (i == 6 || i == 7))
            T_COM[i] = 0;
    }

    smg_disp(t_display[data_disp[index][i]],T_COM[i]);
    i++;
}

